﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Love2DWindow.LoveEmbed;
using System.Windows.Threading;
using System.ComponentModel;

namespace Love2DWindow
{

    /// <summary>
    /// ViewModel
    /// </summary>
    public class GameSceneViewModel : System.ComponentModel.INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;


        public GameScene GameScene => _gameScene;
        private GameScene _gameScene;

        public float Red
        {
            get => _gameScene.Red;
            set
            {
                _gameScene.Red = value;
                RaiseEvent(nameof(Red));
            }
        }

        public float Blue
        {
            get => _gameScene.Blue;
            set
            {
                _gameScene.Blue = value;
                RaiseEvent(nameof(Blue));
            }
        }

        public float Green
        {
            get => _gameScene.Green;
            set
            {
                _gameScene.Green = value;
                RaiseEvent(nameof(Green));
            }
        }

        public float BackRed
        {
            get => _gameScene.BackRed;
            set
            {
                _gameScene.BackRed = value;
                RaiseEvent(nameof(BackRed));
            }
        }

        public float BackBlue
        {
            get => _gameScene.BackBlue;
            set
            {
                _gameScene.BackBlue = value;
                RaiseEvent(nameof(BackBlue));
            }
        }

        public float BackGreen
        {
            get => _gameScene.BackGreen;
            set
            {
                _gameScene.BackGreen = value;
                RaiseEvent(nameof(BackGreen));
            }
        }

        public float Left
        {

            get => _gameScene.Left;
            set
            {
                _gameScene.Left = value;
                RaiseEvent(nameof(Left));
            }
        }

        public float Top
        {

            get => _gameScene.Top;
            set
            {
                _gameScene.Top = value;
                RaiseEvent(nameof(Top));
            }
        }

        public float Width
        {

            get => _gameScene.Width;
            set
            {
                _gameScene.Width = value;
                RaiseEvent(nameof(Width));
            }
        }

        public float Height
        {

            get => _gameScene.Height;
            set
            {
                _gameScene.Height = value;
                RaiseEvent(nameof(Height));
            }
        }

        public void Switch()
        {
            _gameScene.Switch();
        }


        public GameSceneViewModel()
        {
            _gameScene = new GameScene();
        }

        public void RaiseEvent(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        // 句柄
        private IntPtr _childHandle = IntPtr.Zero, _hostHandle = IntPtr.Zero;

        private bool _isSetChildWindow = false;// 是否设置了子窗口。

        private GameSceneViewModel _gameSceneViewModel;

        public MainWindow()
        {
            InitializeComponent();

            _gameSceneViewModel = new GameSceneViewModel();

            Loaded += (s, e) =>
            {
                Love.Boot.Init(new Love.BootConfig() { WindowBorderless = true });
                Love.Window.Close(); // 如果不把空窗口关掉会出现奇怪的事情。

                new Thread(() =>
                {
                    lock (_gameSceneViewModel)
                    {
                        Love.Boot.Run(_gameSceneViewModel.GameScene);
                    }


                }).Start();


                while (!Love.Window.IsOpen()) ;



                TrySetParent();
            };

            DataContext = _gameSceneViewModel;
        }

        private bool TryGetHandle()
        {
            if (_childHandle == IntPtr.Zero)
            {
                var temp = Love.Special.GetWin32Handle();
                if (temp != IntPtr.Zero) _childHandle = temp;
            }

            if (_hostHandle == IntPtr.Zero)
            {
                var temp = this.GetCurrentWindowHandle();
                if (temp != IntPtr.Zero) _hostHandle = temp;
            }

            return _childHandle != IntPtr.Zero && _hostHandle != IntPtr.Zero;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var vm = (sender as FrameworkElement).DataContext as GameSceneViewModel;
            vm.Switch();
        }

        private bool TrySetParent()
        {
            if (TryGetHandle())
            {
                if (_isSetChildWindow == false)
                {
                    Win32.SetParent(_childHandle, _hostHandle);
                    Win32.MoveWindow(_childHandle, 0, 0, 600, 600, true);
                    _isSetChildWindow = true;
                }

                return true;
            }
            return false;

        }


    }
}
